// pages/addBigWheel/index.js
import {
  UserAccountModel
} from '../../models/userAccount.js'
let userAccountModel = new UserAccountModel()
let start = 0
let end = 0

Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime: '2019-12-01 00:00:00',
    endTime:'',
    startYear: 2019,
    endYear: 2025,
    activity: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const eventChannel = this.getOpenerEventChannel()
    var that = this
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log(data.data)
      that.setData({
        activity: data.data,
        startTime: that.getFormatTime(data.data.start_time),
        endTime: that.getFormatTime(data.data.end_time),
      })
      start = data.data.start_time
      end = data.data.end_time
    })
    console.log(this.data.startTime)
    console.log(this.data.endTime)

  },
  getFormatTime(timestamp){
    return new Date(parseInt(timestamp) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ").replace(/\//g, "-").replace(/上午/, "").replace(/下午/, "")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    this.editBigWheel(e.detail.value)

  },
  formReset() {
    console.log('form发生了reset事件')
  },

  _clearForm() {
    this.setData({
      form_info: ''
    })
  },

  editBigWheel(data) {
    console.log(start, end)
    data.start_time = start
    data.end_time = end
    data.rules = data.rules.trim()
    wx.showLoading({
      title: '添加中',
    })
    
    let account = userAccountModel.editBigWheel(data)
    account.then(
      res => {
        console.log(res)
        if (res.code == 1) {
          wx.hideLoading();

          wx.showToast({
            title: '发送成功',
          })
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '发送失败 ' + res.message,
          })

        }
      }
    ).catch(e => {
      wx.hideLoading();
      console.log(e)
    });
  },
  sendTimeToFather(e) {
    var data = e.detail.formatTime
    // console.log(data)
    var format = data.replace(/-/g, '/')
    start = Date.parse(format) / 1000

  },
  sendTimeToFather1(e) {
    var data = e.detail.formatTime
    // console.log(data)
    var format = data.replace(/-/g, '/')
    end = Date.parse(format) / 1000
  },
  uploadImg() {
    var me = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success(res) {
        console.log(res)
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        wx.uploadFile({
          url: 'http://app.deweiyi.com/app/upload/uploadAqie',
          filePath: tempFilePaths[0],
          name: 'img',
          header: {
            'content-type': 'multipart/form-data'
          },
          formData: {
            'type': 'bigwheel'
          },
          success(res) {
            const data = JSON.parse(res.data)
            //do something
            console.log(data.data.picture)
            me.setData({
              'activity.image': data.data.picture
            })
          }
        })
      }
    })
  },
  uploadImg2() {
    var me = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album'],
      success(res) {
        console.log(res)
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths);
        wx.uploadFile({
          url: 'http://app.deweiyi.com/app/upload/uploadAqie',
          filePath: tempFilePaths[0],
          name: 'img',
          header: {
            'content-type': 'multipart/form-data'
          },
          formData: {
            'type': 'bigwheel'
          },
          success(res) {
            const data = JSON.parse(res.data)
            //do something
            console.log(data.data.picture)
            me.setData({
              'activity.image_title': data.data.picture
            })
          }
        })
      }
    })
  }
})